package FightLandlords;

import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Boxes {

    public ArrayList<Card> cardArrayList = new ArrayList<>(52);

    //买一副牌
    public Boxes() {
        String[] color = {"♣","♦","♥","♠"};
        String[] number = {"2","3","4","5","6","7","8","9","10","11","12","13"};
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 4; j++) {
                String c = color[j];
                String n = number[i];
                Card card = new Card(c,n);
                cardArrayList.add(card);
            }
        }
    }

    @Override
    public String toString() {
        return "Boxes{" +
                "cardArrayList=" + cardArrayList +
                '}';
    }

    //准备三位玩家
    public ArrayList<Card> s1 = new ArrayList<>();
    public ArrayList<Card> s2 = new ArrayList<>();
    public ArrayList<Card> s3 = new ArrayList<>();
    public ArrayList<Card> s4 = new ArrayList<>();

    //洗牌
    private void swap(ArrayList<Card> cardArrayList , int i, int j) {
        Card temp = cardArrayList.get(j);
        cardArrayList.set(j,cardArrayList.get(i));
        cardArrayList.set(i,temp);
    }

    public void shuffle() {
        Random random = new Random();

        for (int j = 0; j < cardArrayList.size(); j++) {
            int i = random.nextInt(cardArrayList.size());
            swap(cardArrayList,i,j);
        }
    }

    //开始发牌
    public List<ArrayList<Card>> deal() {
        List<ArrayList<Card>> lists = new ArrayList<>(3);
        lists.add(s1);
        lists.add(s2);
        lists.add(s3);
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardArrayList.remove(0);
                lists.get(i).add(card);
            }
        }


        s3.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return Integer.parseInt(o1.number) - Integer.parseInt(o2.number);
            }
        });

        s2.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return Integer.parseInt(o1.number) - Integer.parseInt(o2.number);
            }
        });

        s1.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return Integer.parseInt(o1.number) - Integer.parseInt(o2.number);
            }
        });
        return lists;
    }
}
